package com.mouse.hxsj.service;

import com.mouse.damo.micro.*;
import com.mouse.haoi.service.CustomService;
import com.mouse.hxsj.entity.enums.MapPlace;
import com.mouse.hxsj.util.CommonUtil;
import com.mouse.hxsj.util.JsonUtil;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

@Component
public class RobberService {
    static CustomService customService = new CustomService();
    static Com baseCom = new Com();
    static List<Integer> turnPoint = Arrays.asList(8, 31);

    public static void main(String[] args) {
        Com com = new Com();
        int hwnd = 132844;
        Press press = new Press(com);
        FindPic findPic = new FindPic(com);
        Mouse mouse = new Mouse(com);
        Window window = new Window(com);
        int[] windowRect = window.getClientSize(hwnd);
        int height = windowRect[1], width = windowRect[2];
        com.setResourcePath("D:\\damo");
        com.setDict(0, "坐标.txt");
        boolean bind = com.bind(hwnd, "dx2", "windows3", "windows", 0, true);
        System.out.println(bind);
        if (false) {
            MapPlace yunyan = MapPlace.yunyan;
            MapPlace.fly(com, yunyan);
            boolean flag = customService.loop(10, () ->
                    yunyan.getMap().equals(CommonUtil.getMap(com).getMap()));
        }
//        if (!flag) {
        if (true) {
            String map = CommonUtil.getMap(com).getMap();
            press.keyPress(Press.M);
            Sleep.sleep(200);
            boolean mFlag = customService.loop(5, () -> findPic.findPic(0, 0, height, width,
                    "login/场景地图.bmp", "203040", 0.8, 0)[0] != -1);
            if (!mFlag) {
                press.keyPress(Press.M);
                Sleep.sleep(200);
            }
            int[] pic = findPic.findPic(0, 0, height, width,
                    "bao/传送.bmp", "203040", 0.8, 0);
            System.out.println(JsonUtil.toJson(pic));
            mouse.mouseMoveTo(pic[1] + 5, pic[2] + 5);
            pic = getAbsolutePoint(hwnd, pic);

            System.out.println(JsonUtil.toJson(pic));
//            mouse.mouseDoubleClick(pic[1] + 5, pic[2] + 5);
            press.keyPress(Press.M);
            customService.loop(60, () -> !map.equals(CommonUtil.getMap(com).getMap()));
            System.out.println("OK");
        }
    }

    public static int[] getAbsolutePoint(int hwnd, int[] data) {
        int result[] = {0, 0, 0};
        Window window = new Window(baseCom);
        int[] windowRect = window.getWindowRect(hwnd);
        if (data[1] == -1 || windowRect[0] < 0) {
            result[0] = -1;
            result[1] = -1;
            result[2] = -1;
            return result;
        }
        result[0] = 1;
        result[1] = windowRect[1] + data[1] + turnPoint.get(0);
        result[2] = windowRect[2] + data[2] + turnPoint.get(1);
        return result;
    }

    public static void clickAbsolutePoint(int hwnd) {

    }
}
